package record.type;

import java.util.*;

/**
 * @author 小傅哥，微信：fustack
 * @description 基本类型注册器
 * @github https://github.com/fuzhengwei
 * @Copyright 公众号：bugstack虫洞栈 | 博客：https://bugstack.cn - 沉淀、分享、成长，让自己和他人都能有所收获！
 */
public class SimpleTypeRegistry {

    private static final Set<Class<?>> SIMPLE_TYPE_SET = new HashSet<Class<?>>();
    private static final Map<Class<?>, TypeHandler> CUSTOM_TYPE_SET = new HashMap<>();

    static {
        setTypeHandler();
        setSimpleTypeHandler();
    }

    private SimpleTypeRegistry() {
        // Prevent Instantiation
    }

    /*
     * Tells us if the class passed in is a known common type
     *
     * @param clazz The class to check
     * @return True if the class is known
     */

    public static void setSimpleTypeHandler() {
        SIMPLE_TYPE_SET.add(String.class);
        SIMPLE_TYPE_SET.add(short.class);
        SIMPLE_TYPE_SET.add(Short.class);
        SIMPLE_TYPE_SET.add(char.class);
        SIMPLE_TYPE_SET.add(Character.class);
        SIMPLE_TYPE_SET.add(int.class);
        SIMPLE_TYPE_SET.add(Integer.class);
        SIMPLE_TYPE_SET.add(long.class);
        SIMPLE_TYPE_SET.add(Long.class);
        SIMPLE_TYPE_SET.add(float.class);
        SIMPLE_TYPE_SET.add(Float.class);
        SIMPLE_TYPE_SET.add(double.class);
        SIMPLE_TYPE_SET.add(Double.class);
        SIMPLE_TYPE_SET.add(boolean.class);
        SIMPLE_TYPE_SET.add(Boolean.class);
        SIMPLE_TYPE_SET.add(Date.class);
    }

    public static boolean isSimpleType(Class<?> clazz) {
        return SIMPLE_TYPE_SET.contains(clazz);
    }

    public static void setTypeHandler() {
        CUSTOM_TYPE_SET.put(CommonOperateTypeHandler.class, new CommonOperateTypeHandler());
        CUSTOM_TYPE_SET.put(PriorityTypeHandler.class, new PriorityTypeHandler());
        CUSTOM_TYPE_SET.put(RuleTypeHandler.class, new RuleTypeHandler());
    }

    public static TypeHandler getTypeHandler(Class<?> clazz) {
        TypeHandler typeHandler = CUSTOM_TYPE_SET.get(clazz);
        if (typeHandler == null) {
            return CUSTOM_TYPE_SET.get(CommonOperateTypeHandler.class);
        }
        return typeHandler;
    }
}
